package com.webmagic.demo.mv;

import com.xiaoleilu.hutool.util.ReUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.Date;
import java.util.List;

/**
 * @description
 * @Auther zhanglu
 * @Date 2018/4/17 上午1:28
 */
public class MvProcessor implements PageProcessor {
    private Site site = Site.me().setCharset("GB2312").setRetryTimes(3).setSleepTime(100);

    private static final String URL_POST = "(http://s\\.dydytt\\.net/html/gndy/(\\w+)/(\\d+)/(\\d+)\\.html)";

    @Override
    public void process(Page page) {
        List<String> pathList = page.getHtml().xpath("//*[@id=\"header\"]/div/div[3]/div[3]/div[2]/div[2]/div[2]/ul").links().regex(URL_POST).all();
        page.addTargetRequests(pathList);

        /*String keyword = ReUtil.get("(keyword=).*", page.getUrl().toString(), 0);

        if(keyword != null){
            page.putField("keyword", keyword.replace("keyword=",""));
        }*/

        if (page.getUrl().regex(URL_POST).match()) {
            Selectable content = page.getHtml().xpath("//div[@id=\"Zoom\"]/span");

            //远程页面获取的数据
            String[] cntAry = ReUtil.get("(年　　代).*(<strong>)", content.toString(), 0).split("<br>◎");

            String time = "";
            String place = "";
            String category = "";
            String director = "";
            String star = "";
            String description = "";

            for (int i = 0; i < cntAry.length; i++) {
                if (cntAry[i].indexOf("年　　代") == 0) {
                    time = cntAry[i].replace("年　　代", "");
                }
                if (cntAry[i].indexOf("产　　地") == 0) {
                    place = cntAry[i].replace("产　　地", "");
                }
                if (cntAry[i].indexOf("类　　别") == 0) {
                    category = cntAry[i].replace("类　　别", "");
                }
                if (cntAry[i].indexOf("导　　演") == 0) {
                    director = cntAry[i].replace("导　　演", "");
                }
                if (cntAry[i].indexOf("主　　演") == 0) {
                    star = cntAry[i].replace("主　　演", "");
                }
                if (cntAry[i].indexOf("简　　介") == 0) {
                    description = cntAry[i].replace("简　　介", "").replace("<strong>", "");
                }
            }

            String title = page.getHtml().xpath("//div[@class=\"title_all\"]/h1/font/text()").toString();
            String topImgUrl = content.css("img", "src").toString();
            String downPath = content.css("a", "href").toString();

            MoveDO move = new MoveDO();
            move.setTitle(title);
            move.setTime(time);
            move.setTopImgUrl(topImgUrl);
            move.setPlace(place);
            move.setCategory(category);
            move.setDirector(director);
            move.setStar(star);
            move.setDescription(description);
            move.setCreateDate(new Date());
            move.setCount(0);
            move.setStatus(1);
            move.setDownPath(downPath);

            if (StrUtil.isBlank(move.getDownPath())) {
                page.setSkip(true);
            } else {
                page.putField("bean", move);
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
